﻿$(function(){
    $("#myFile").change(ajaxFileUpload);
});
 function ajaxFileUpload(){
        //starting setting some animation when the ajax starts and completes
        var fileId = $(this).attr("name");
        if(!fileId) return;
        $("#loading")
        .ajaxStart(function(){
            $(this).show();
        })
        .ajaxComplete(function(){
            var o = $(this);
            o.children().eq(0).attr("src","images/ok.png");
            setTimeout(function(){
                o.hide();
                o.children().eq(0).attr("src","images/loading.gif");
            },2000);
        });
        /*
            prepareing ajax file upload
            url: the url of script file handling the uploaded files
                        fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
            dataType: it support json, xml
            secureuri:use secure protocol
            success: call back function when the ajax complete
            error: callback function when the ajax failed
            
                */
        $.ajaxFileUpload
        (
            {
                url:'AjaxHandler.ashx?action=uploadf&random=' + Math.random(),
                secureuri:false,
                fileElementId:fileId,
                dataType: 'json',
                success: function (data, status)
                {
                    console.log(arguments);
                },
                error: function (data, status, e)
                {
                   console.log(arguments);
                }
            }
        )
        $("#" + fileId).change(ajaxFileUpload);
        return true;

    }